﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmDM_Table : Form
    {
        EnumGUI.ThaoTac thaotac;
        DM_TABLE_Provider prov = null;
        DM_TABLE Currobj = null;
        private SqlConnection con;

        public frmDM_Table(DM_TABLE _obj, EnumGUI.ThaoTac _thaotac)
        {
            InitializeComponent();
            this.thaotac = _thaotac;
            this.Currobj = _obj;
        }
        private void frmDM_Table_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        private void LoadAll()
        {
            con = (SqlConnection)StaticVar.ServerConnection;
            prov = new DM_TABLE_Provider();
            LoadData();
            ControlUI(thaotac);
        }
        public void LoadData()
        {
            LoadToCmb_Area();
        }

        private void LoadToCmb_Area()
        {
            try
            {
                DM_AREAList list = StaticVar.DanhSachKhuVuc;
                cmbArea.DataSource = null;

                if (list.Count==0)
                {
                    if (MessageBox.Show("Chưa có danh sách khu vực!\nVui lòng nhập danh sách khu vực trước!\nBạn có muốn nhập thêm khu vực không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_Area Area = new frmDM_Area(null, EnumGUI.ThaoTac.them);
                        if (Area.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachKhuVuc;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("Khu vực", typeof(string));
                foreach (DM_AREA obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbArea.DataSource = data;
                cmbArea.DisplayMember = "Khu vực";
                cmbArea.ValueMember = "Mã";
                cmbArea.ColumnWidths = "0,200";
                cmbArea.SelectedIndex = -1;
                cmbArea.Text = "";
            }
            catch { }
        }
        private void ControlUI(EnumGUI.ThaoTac thaotac)
        {
            if (thaotac == EnumGUI.ThaoTac.them || thaotac == EnumGUI.ThaoTac.sua)
            {
                SetTextReadOnly(false);
            }
            if (thaotac == EnumGUI.ThaoTac.xem)
            {
                SetTextReadOnly(true);
            }
            SetStateButton(thaotac);
            loadToUI(Currobj);
            UIHelper.LoadFocus(txtName);
        }
        private void SetStateButton(EnumGUI.ThaoTac thaotac)
        {
            switch (thaotac)
            { 
                case EnumGUI.ThaoTac.them:
                      btnLuuThem.Visible = true;
                      btnLuuThoat.Visible = true;
                      btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.sua:
                      btnLuuThem.Visible = false;
                      btnLuuThoat.Visible = true;
                      btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.xem:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = false;
                    btnMacDinh.Visible = false;
                    break;
            }
          
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtName.ReadOnly = IsReadOnly;
            txtNote.ReadOnly = IsReadOnly;
            cmbArea.Enabled = !IsReadOnly;
            dtTime1.Enabled = !IsReadOnly;
            dtTime2.Enabled = !IsReadOnly;
            dtTime3.Enabled = !IsReadOnly;
            dtTime4.Enabled = !IsReadOnly;
            numPrice1.Enabled = !IsReadOnly;
            numPrice2.Enabled = !IsReadOnly;
            numPrice3.Enabled = !IsReadOnly;
            numPrice4.Enabled = !IsReadOnly;
                
        }

        private bool loadToUI(DM_TABLE obj)
        {
            bool bKQ = false;
            try
            {
                if (obj == null)
                {
                    obj = new DM_TABLE();
                    DM_TABLEList list = prov.GetAll();
                    if (list == null) obj.ID = "AB.1";
                    else obj.ID = "AB." + (list.Count + 1).ToString();
                    obj.IDPOS = StaticVar.CPUID;
                    obj.Time1 = DateTime.Now;
                    obj.Time2 = DateTime.Now;
                    obj.Time3 = DateTime.Now;
                    obj.Time4 = DateTime.Now;
                }

                txtID.Text = obj.ID;
                labPosID.Text = obj.IDPOS;
                txtName.Text = obj.Name;
                txtNote.Text = obj.Note;

                dtTime1.Value = obj.Time1;
                dtTime2.Value = obj.Time2;
                dtTime3.Value = obj.Time3;
                dtTime4.Value = obj.Time4;

                numPrice1.Value = (Decimal)obj.Price;
                numPrice2.Value = (Decimal)obj.Price2;
                numPrice3.Value = (Decimal)obj.Price3;
                numPrice4.Value = (Decimal)obj.Price4;

                if (numPrice1.Value == 0) numPrice1.ResetText();
                if (numPrice2.Value == 0) numPrice2.ResetText();
                if (numPrice3.Value == 0) numPrice3.ResetText();
                if (numPrice4.Value == 0) numPrice4.ResetText();

                if (StaticVar.DanhSachKhuVuc != null && StaticVar.DanhSachKhuVuc.Count > 0)
                {
                    DM_AREA objArea = StaticVar.DanhSachKhuVuc.Find(obj.AreaID);
                    if (objArea != null) cmbArea.Text = objArea.Name;
                }

                bKQ = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return bKQ;
        }
        private DM_TABLE loadToValues()
        {
            DM_TABLE obj = new DM_TABLE();
            try
            {
                if (labPosID.Text != "0" && txtName.Text != "" && labAreaID.Text!="0")
                {
                    obj.Fill(txtID.Text, txtName.Text, labAreaID.Text, txtNote.Text, short.Parse(labStatus.Text), dtTime1.Value, (double)numPrice1.Value, dtTime2.Value, (double)numPrice2.Value, dtTime3.Value, (double)numPrice3.Value, dtTime4.Value, (double)numPrice4.Value, dtTime4.Value, (double)numPrice4.Value, labPosID.Text);
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    obj = null;
                }
            }
            catch
            {
                obj = null;
            }
            return obj;
        }
        private bool cmd_XuLy(EnumGUI.ThaoTac thaotac)
        {
            bool KQ = false;
            if (thaotac == EnumGUI.ThaoTac.macdinh) KQ = loadToUI(Currobj);
            else
            {
                DM_TABLE obj = loadToValues();
                if (obj != null)
                {
                    switch (thaotac)
                    {
                        case EnumGUI.ThaoTac.them:
                            if (obj != null) KQ = prov.Cmd_ThemDM_TABLE(obj);
                            break;
                        case EnumGUI.ThaoTac.sua:
                            if (obj != null) KQ = prov.Cmd_CapNhatDM_TABLE(obj);
                            break;
                    }
                    if (prov.ErrMsg != null)
                        MessageBox.Show(prov.ErrMsg.Replace("DM_TABLE", "bàn"), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            return KQ;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            switch (_thaotac)
            { 
                case "luuthoat":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        this.DialogResult = DialogResult.OK;
                        base.Close();
                    }
                    break;
                case "luuthem":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        ControlUI(EnumGUI.ThaoTac.them);
                    }
                    break;
                case "macdinh":
                    KQ = cmd_XuLy(EnumGUI.ThaoTac.macdinh);
                    break;
                case "themkhuvuc":
                     frmDM_Area Area = new frmDM_Area(null, EnumGUI.ThaoTac.them);
                     if (Area.ShowDialog() == DialogResult.OK)
                     {
                         LoadToCmb_Area();
                     }
                    break;
                case "thoat":
                    this.DialogResult = DialogResult.OK;
                    base.Close();
                    break;
            }
            StaticVar.DanhSachBan = prov.GetAll();
            UIHelper.LoadFocus(txtName);
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtName.Name)
                    UIHelper.txt_KeyPress(sender, cmbArea, e);
                if (txt.Name == cmbArea.Name)
                    UIHelper.txt_KeyPress(sender, txtNote, e);
                if (txt.Name == txtNote.Name)
                    UIHelper.txt_KeyPress(sender, dtTime1, e);
                if (txt.Name == dtTime1.Name)
                    UIHelper.txt_KeyPress(sender, numPrice1, e);
                if (txt.Name == numPrice1.Name)
                    UIHelper.txt_KeyPress(sender, dtTime2, e);
                if (txt.Name == dtTime2.Name)
                    UIHelper.txt_KeyPress(sender, numPrice2, e);
                if (txt.Name == numPrice2.Name)
                    UIHelper.txt_KeyPress(sender, dtTime3, e);
                if (txt.Name == dtTime3.Name)
                    UIHelper.txt_KeyPress(sender, numPrice3, e);
                if (txt.Name == numPrice3.Name)
                    UIHelper.txt_KeyPress(sender, dtTime4, e);
                if (txt.Name == dtTime4.Name)
                    UIHelper.txt_KeyPress(sender, numPrice4, e);
                if (txt.Name == numPrice4.Name)
                    UIHelper.txt_KeyPress(sender, btnLuuThoat, e);
            }
            catch { }
        }

        private void dtTime_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                labTime1.Text = dtTime1.Value.ToString("hh:mm");
                labTime2.Text = dtTime2.Value.ToString("hh:mm");
                labTime3.Text = dtTime3.Value.ToString("hh:mm");
            }
            catch { }
        }

        private void cmbArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbArea.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbArea.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labAreaID.Text = Ma;
                cmbArea.Text = Ten;
            }
        }

        private void cmbArea_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbArea.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbArea.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labAreaID.Text = Ma;
                cmbArea.Text = Ten;
            }
        }
    }
}
